{extend name="layout/base"}

{block name="content"}

<table lay-filter="table" id="table" class="table"></table>

<script type="text/html" id="toolbar">
    <input class="layui-input" name="key" id="key" placeholder="请输入文章标题">
    <div class="layui-btn-container">
        <button class="layui-btn" id="search">搜索</button>
        <button class="layui-btn" id="add"><i class="layui-icon">&#xe608;</i>添加</button>
        <button class="layui-btn" lay-event="endFlow"><i class="layui-icon">&#xe608;</i>终止</button>
        <button class="layui-btn" lay-event="cancelFlow"><i class="layui-icon">&#xe608;</i>取消</button>
    </div>
</script>

<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
    <a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
</script>


<script>
    layui.use(['jquery', 'table', 'layer'], function ($, table, layer) {
        var opt = {
            elem: '#table',
            url: "{:url('')}",
            method: 'post',
            page: true,
            toolbar: '#toolbar',
            cols: [[
                {type: 'radio', fixed: 'left'},
                {field: 'id', title: 'ID'},
                {field: 'uid', title: '用户id'},
                {field: 'name', title: '标题'},
                {field: 'statusBtn', title: '状态'},
                {field: 'uptime', title: '时间'},
                {title: '操作', toolbar: '#tool'}
            ]],
            where: {}
        }, tabIns = table.render(opt);

        $('#search').on('click', function() {
            var key = $('#key').val();
            var type = $('#type').val();
            tabIns.reload({
                where: {key: key,type: type}
            });
        });

        $('#add').on('click', function () {
            // location.href = "{:url('news/add')}"
            layer.open({
                type: 2,
                area: ['50%', '50%'],
                title: '添加',
                content: "{:url('add')}"
            })
        });

        //头工具栏事件
        table.on('toolbar(table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch(obj.event){
                case 'cancelFlow':
                    var data = checkStatus.data[0];  //获取选中行数据
                    if(data==''||data==undefined){
                        layer.msg('请选择您要操作的单据~~');return;
                    }
                    var id = data['id'];
                    var status = data['status'];

                    if(status==2){
                        var index = layer.load(1, {
                            shade: [0.1,'#fff'] //0.1透明度的白色背景
                        });
                        $.ajax({
                            type: "GET",
                            url: "{:url('./wf/wfdo')}?act=cancelflow&bill_id="+id+'&bill_table=news',
                            dataType: "json",
                            success: function(data){
                                if(data.code==1){
                                    layer.close(index);
                                    layer.alert(data.msg);
                                }else{
                                    layer.msg(data.msg,{icon:1,time: 1500},function(){
                                        window.location.reload();
                                    });
                                }
                            }
                        });
                    }else{
                        layer.msg('选中的单据已经被审批或者未发起工作流~~');return;
                    }
                    break;
                case 'endFlow':
                    var data = checkStatus.data[0];  //获取选中行数据
                    if(data==''||data==undefined) {
                        layer.msg('请选择您要操作的单据~~');
                        return;
                    }
                    var id = data['id'];
                    var status = data['status'];
                    if (status == 1) {
                        var index = layer.load(1, {
                            shade: [0.1, '#fff'] //0.1透明度的白色背景
                        });
                        $.ajax({
                            type: "GET",
                            url: "{:url('./wf/wfdo')}?act=endflow&bill_id=" + id + '&bill_table=news',
                            dataType: "json",
                            success: function (data) {
                                if (data.code == 1) {
                                    layer.close(index);
                                    layer.alert(data.msg);
                                } else {
                                    window.location.reload();
                                }
                            }
                        });
                    } else {
                        layer.msg('选中的单据已经被审批或者未发起工作流~~');
                        return;
                    }
                    break;
            }
        });

        table.on('tool(table)', function (obj) {
            var id = obj.data.id;
            var status = obj.data.status;

            switch (obj.event) {
                case 'del':
                    layer.confirm('确定要删除吗', function (index) {
                        $.get("{:url('news/delete')}", {id: id});
                        layer.close(index);
                        tabIns.reload(opt)
                    });
                    break;
                case 'edit':
                    if(status == 0 || status == -1 ) {
                        layer.open({
                            type: 2,
                            area: ['35%', '35%'],
                            title: '编辑',
                            content: "{:url('news/edit')}?id=" + id
                        })
                    }else{
                        layer.msg('流程审核中或完成不能修改');return;
                    }
                    break;
                case 'check':
                    // switch (status) {
                    //     case 0:
                    //         $.post("{:url('./wf/wfdo')}", {act: 'start',wf_type: 'news','wf_fid' :id, 'wf_id':2,'check_con': '同意'});
                    //         layer.open({
                    //             type: 2,
                    //             area: ['100%', '100%'],
                    //             title: '审核单据信息',
                    //             content: "{:url('./wf/wfdo')}?act=do&wf_type=news&wf_fid=" + id
                    //         });
                    //         break;
                    //     case 1:
                    //         layer.open({
                    //             type: 2,
                    //             area: ['100%', '100%'],
                    //             title: '审核单据信息',
                    //             content: "{:url('./wf/wfdo')}?act=do&wf_type=news&wf_fid=" + id
                    //         });
                    //         break;
                    //     case 100:
                    //
                    //         break;
                    //     default:
                    //         break;
                    // }
                default:
                    break;
            }
        });
    });
</script>
<script type="text/javascript" src="/static/work/workflow.5.0.js" ></script>
{/block}
